<?php

class ibizatv extends baseController {
	public function __construct() {
		parent::__construct();
		$this->controller = get_class($this);
	}
	function index(){
		$db_obj = $this->getDbObj();
		
		if ( null == SessionUtil::getVar('ui_view_show_setting') ) {
			SessionUtil::setVar('ui_view_show_setting','Y');
		}
		$conditions = array();
		$this->show_setting = SessionUtil::getVar('ui_view_show_setting');
		if ( 'all' == $this->show_setting ) {
		
		} else if ( 'Y' == $this->show_setting ) {
			$conditions['active'] = 'Y';
		} else if ( 'N' == $this->show_setting ) {
			$conditions['active'] = 'N';
		}
		$conditions = count($conditions) > 0 ? $conditions : null;
		$tmp_sub_catalog_list = $db_obj->findAll($conditions,'`order` ASC');
		
		$resource_obj = spClass("model_resource");
		foreach($tmp_sub_catalog_list as &$row_data) {
			$resource_data_row = $resource_obj->get ( $row_data ['resource_uuid'] );
			$video_file_path = ASSETS_DIR.'/'.$resource_data_row ['path'];
			$row_data['file_size'] = '';
			if( strlen($resource_data_row ['path']) > 0 AND file_exists($video_file_path) ) {
				$file_size = filesize($video_file_path);
				$file_size = $file_size/(1024*1024);
				$row_data['file_size']  = sprintf("%0.2f M",$file_size);
			}
		}
		$this->data_list = $tmp_sub_catalog_list;
	}
	function edit(){
		$uuid = $this->spArgs("uuid");
		$name_en = $this->spArgs("name_en");
		$name_cn = $this->spArgs("name_cn");
		$db_obj = $this->getDbObj();
		//post form
		if ( $name_en ){
			$exist_data = $db_obj->get($uuid);
			$is_new = FALSE;
			if ( !$exist_data ){
				$exist_data = array();
				$is_new = TRUE;
			}
			$sort = intval($this->spArgs("sort"));
			$active = $this->spArgs("active");
			$ajaxf_video = $this->spArgs("ajaxf_video_value");
			$ftp_file_name = $this->spArgs ( "ftp_file_name" );
			if (   $is_new
					OR $exist_data['name_en'] != $name_en
					OR  $exist_data['name_cn'] != $name_cn
					OR $exist_data['order'] != $sort
					OR $exist_data['active'] != $active
					OR strlen($ajaxf_video) > 0
					OR strlen($ftp_file_name) > 0
			){
		
				$exist_data['name_en'] = $name_en;
				$exist_data['name_cn'] = $name_cn;
				$exist_data['order'] = $sort;
				$exist_data['active'] = $active;
		
				$tmp_file_path = FALSE;
				if ( strlen($ajaxf_video) > 0) {
				
					$tmp_file_path =  UPLOAD_TMP_DIR.'/' . $ajaxf_video;
				} elseif ( strlen($ftp_file_name) > 0) {
					$tmp_file_path = 'ftp/'.$ftp_file_name;
				}
				
				if ( $tmp_file_path ) {
					$resource_obj = spClass ( "model_resource" );
					if (strlen ( $exist_data ['resource_uuid'] ) > 0) {
						$resource_obj->updateByTmpFile ( $tmp_file_path, $exist_data ['resource_uuid'] );
					} else {
						$new_resource_uuid = $resource_obj->createByTmpFile ( $tmp_file_path );
						$exist_data ['resource_uuid'] = $new_resource_uuid;
					}
				}
				if ( $is_new ) {
					$db_obj->create($exist_data);
				} else {
					$db_obj->update(array($db_obj->pk=>$exist_data[$db_obj->pk]),$exist_data);
				}
			}
			$this->jump(spUrl($this->controller, 'index'));
		}
		
		$this->session_id = session_id();
		$this->session_name = session_name();
		$this->data = $db_obj->get($uuid);
		
		//ftp file list
		$fpt_file_list = array();
		$dp = opendir("ftp");
		//List files in images directory
		while (($file_name = readdir($dp)) !== false)
		{
			if ( $file_name == '.' OR $file_name == '..' )
				continue;
			$ext = Util::getFileExt($file_name);
			if ( strtolower($ext) == 'mp4' ) {
				$file_size = filesize('ftp/'.$file_name);
				$file_size /= 1000000;
				$fpt_file_list[] = array('name'=>$file_name,'size'=>sprintf("%0.2f M",$file_size));
			}
		}
		closedir($dp);
		$this->fpt_file_list = $fpt_file_list;
	}
	function delete(){
		$uuid = $this->spArgs("uuid");
		$db_obj = $this->getDbObj();
		$db_obj->model_version_delete_byPk($uuid,true,true);
		$this->jump(spUrl($this->controller, 'index'));
	}
	function setshowsetting(){
		$value = $this->spArgs("v");
		SessionUtil::setVar('ui_view_show_setting',$value);
		$this->jump(spUrl('ibizatv','index'));
	}
	
	private function getDbObj() {
		$db_obj = spClass("model_".get_class($this));
		return $db_obj;
	}
}

?>